package org.example.securevideotransmission.model;

import org.bytedeco.javacv.*;

import java.awt.image.BufferedImage;

import static org.bytedeco.javacv.Java2DFrameUtils.toBufferedImage;

public class VideoProcessor {

    public static void processVideos(String secretVideoPath, String originalVideoPath, String outputVideoPath) throws Exception {
        try (FFmpegFrameGrabber secretGrabber = new FFmpegFrameGrabber(secretVideoPath);
             FFmpegFrameGrabber originalGrabber = new FFmpegFrameGrabber(originalVideoPath)) {

            secretGrabber.start();
            originalGrabber.start();

            try (FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(outputVideoPath,
                    originalGrabber.getImageWidth(),
                    originalGrabber.getImageHeight())) {

                recorder.setFrameRate(originalGrabber.getFrameRate());
                recorder.setVideoCodec(originalGrabber.getVideoCodec());
                recorder.setFormat("mp4");
                recorder.start();

                while (true) {
                    Frame originalFrame = originalGrabber.grabImage();
                    Frame secretFrame = secretGrabber.grabImage();

                    if (originalFrame == null || secretFrame == null) {
                        break;
                    }

                    // 对涉密帧进行加密
                    BufferedImage encryptedFrame = encryptFrame(toBufferedImage(secretFrame));

                    // 融合帧
                    BufferedImage mergedFrame = mergeFrames(toBufferedImage(originalFrame), encryptedFrame);

                    // 写入输出视频
                    recorder.record(Java2DFrameUtils.toFrame(mergedFrame));
                }
            }

            secretGrabber.stop();
            originalGrabber.stop();
        }
    }

    private static BufferedImage encryptFrame(BufferedImage frame) {
        int width = frame.getWidth();
        int height = frame.getHeight();

        BufferedImage encrypted = new BufferedImage(width, height, frame.getType());

        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                int pixel = frame.getRGB(x, y);
                int encryptedPixel = ~pixel; // 简单加密（按位取反）
                encrypted.setRGB(x, y, encryptedPixel);
            }
        }
        return encrypted;
    }

    private static BufferedImage mergeFrames(BufferedImage original, BufferedImage encrypted) {
        int width = original.getWidth();
        int height = original.getHeight();

        BufferedImage merged = new BufferedImage(width, height, original.getType());

        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                int originalPixel = original.getRGB(x, y);
                int encryptedPixel = encrypted.getRGB(x, y);

                // 融合操作（加权平均）
                int r = (int) (((originalPixel >> 16 & 0xFF) * 0.8) + ((encryptedPixel >> 16 & 0xFF) * 0.2));
                int g = (int) (((originalPixel >> 8 & 0xFF) * 0.8) + ((encryptedPixel >> 8 & 0xFF) * 0.2));
                int b = (int) (((originalPixel & 0xFF) * 0.8) + ((encryptedPixel & 0xFF) * 0.2));

                int mergedPixel = (r << 16) | (g << 8) | b;
                merged.setRGB(x, y, mergedPixel);
            }
        }

        return merged;
    }
}
